<?php
namespace dwc_app;

class SpreadsheetsArchiveUploader
{
    public $dwca;
    public $spreadsheets_api;
    public $docs_api;
    
    function __construct($dwca, $username, $password)
    {
        $this->dwca = $dwca;
        
        $this->spreadsheets_api = new GoogleSpreadsheetsAPI($username, $password, @$_SESSION['GOOGLE_AUTH_TOKEN'], 'tdwgcodeblitz-dwc_spreadsheets-0.1');
        $this->docs_api = new GoogleDocsAPI($username, $password, @$_SESSION['GOOGLE_AUTH_TOKEN'], 'tdwgcodeblitz-dwc_docs-0.1');
    }
    
    public function send_core_to_spreadsheets()
    {
        $this->send_core_or_extension_as_csv($this->dwca->core);
        $this->send_extensions_to_spreadsheets();
        // $table_fields = array();
        // foreach($this->dwca->core->fields as $field)
        // {
        //     if(preg_match("/\/([^\/]+)$/", $field['term'], $arr))
        //     {
        //         $table_fields[] = $arr[1];
        //     }
        // }
        // $table_feed_id = $this->spreadsheets_api->create_worksheet('DarwinCoreTest', $table_fields);
        // $table_feed_id = str_replace("/tables/", "/records/", $table_feed_id);
        // // // $table_id = 112345;
        // $this->send_core_taxa_to_spreadsheets($table_feed_id, $table_fields);
        // //echo "<br><a href=http://tables.googlelabs.com/DataSource?dsrcid=$table_id>See the table</a><br>";
    }
    
    public function send_extensions_to_spreadsheets()
    {
        foreach($this->dwca->extensions as $extension)
        {
            $this->send_core_or_extension_as_csv($extension);
        }
    }
    
    public function send_core_or_extension_as_csv($core_or_extension)
    {
        $spreadsheet_name = $core_or_extension->row_type;
        if(preg_match("/\/([^\/]+)$/", $spreadsheet_name, $arr))
        {
            $spreadsheet_name = $arr[1];
        }
        
        $body = file_get_contents($core_or_extension->file_uri);
        if($core_or_extension->fields_enclosed_by)
        {
            // ...',,'... => ...','','...
            $body = str_replace($core_or_extension->fields_terminated_by.$core_or_extension->fields_terminated_by, $core_or_extension->fields_terminated_by.$core_or_extension->fields_enclosed_by.$core_or_extension->fields_enclosed_by.$core_or_extension->fields_terminated_by, $body);
            // ...',,'... => ...','','...
            $body = str_replace($core_or_extension->fields_terminated_by.$core_or_extension->fields_terminated_by, $core_or_extension->fields_terminated_by.$core_or_extension->fields_enclosed_by.$core_or_extension->fields_enclosed_by.$core_or_extension->fields_terminated_by, $body);
            // ...',\n => ...',''\n
            $body = str_replace($core_or_extension->fields_terminated_by.$core_or_extension->lines_terminated_by, $core_or_extension->fields_terminated_by.$core_or_extension->fields_enclosed_by.$core_or_extension->fields_enclosed_by.$core_or_extension->lines_terminated_by, $body);
            // ...','... => ...\t...
            $body = str_replace($core_or_extension->fields_enclosed_by.$core_or_extension->fields_terminated_by.$core_or_extension->fields_enclosed_by, "\t", $body);
        }elseif($fields_terminated_by != "\t")
        {
            $body = str_replace($fields_terminated_by, "\t", $body);
        }
        
        $lines = explode($core_or_extension->lines_terminated_by, $body);
        unset($body);
        $batch = "";
        $count = 0;
        foreach($lines as $line)
        {
            if(substr($line, 0, 1) == $core_or_extension->fields_enclosed_by)
            {
                $line = substr($line, 1);
            }
            if(substr($line, -1) == $core_or_extension->fields_enclosed_by)
            {
                $line = substr($line, 0, -1);
            }
            $batch .= $line."\n";
            $count++;
            if($count>=2500)
            {
                $this->docs_api->create_spreadsheet_from_csv($spreadsheet_name, $batch);
                $batch = "";
                $count = 0;
                break;
            }
        }
        //if($batch) $this->docs_api->create_spreadsheet_from_csv($batch);
    }
    
    
    public function send_core_or_extension_taxa_to_spreadsheets($table_feed_id, $table_fields, $core_or_extension)
    {
        $file_contents = get_remote_file($core_or_extension->file_uri);
        $lines = explode($core_or_extension->lines_terminated_by, $file_contents);
        
        $row_data = array();
        $line_num = 0;
        foreach($lines as $line)
        {
            $line_num++;
            if($core_or_extension->ignore_header_lines && $line_num == 1) continue;
            if(!trim($line)) continue;
            if($core_or_extension->fields_enclosed_by)
            {
                $line = str_replace($core_or_extension->fields_terminated_by.$core_or_extension->fields_terminated_by, $core_or_extension->fields_terminated_by.$core_or_extension->fields_enclosed_by.$core_or_extension->fields_enclosed_by.$core_or_extension->fields_terminated_by, $line);
                $line = str_replace($core_or_extension->fields_terminated_by.$core_or_extension->fields_terminated_by, $core_or_extension->fields_terminated_by.$core_or_extension->fields_enclosed_by.$core_or_extension->fields_enclosed_by.$core_or_extension->fields_terminated_by, $line);
                if(substr($line, -1) == $core_or_extension->fields_terminated_by)
                {
                    $line .= $core_or_extension->fields_enclosed_by.$core_or_extension->fields_enclosed_by;
                }
            }
            $line = preg_replace("/^".preg_quote($core_or_extension->fields_enclosed_by, "/")."/", "", $line);
            $line = preg_replace("/".preg_quote($core_or_extension->fields_enclosed_by, "/")."$/", "", $line);
            $fields = explode($core_or_extension->fields_enclosed_by.$core_or_extension->fields_terminated_by.$core_or_extension->fields_enclosed_by, $line);
            if($fields)
            {
                $record = '<entry xmlns="http://www.w3.org/2005/Atom"
                    xmlns:gs="http://schemas.google.com/spreadsheets/2006">
                    <title>'.$line_num.'</title>
                ';
                foreach($table_fields as $key => $field_name)
                {
                    if($fields[$key]) $record .= "<gs:field name='$field_name'>".$fields[$key]."</gs:field>\n";
                }
                $record .= "</entry>";
                $this->spreadsheets_api->create_record($table_feed_id, $record);
                if($line_num%10 == 0) sleep(2);
            }
        }
    }
}

?>